home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #1 / Ham Radio 2000.iso / ham2000 / hf / dsp / source / pam.old < prev    next >
Encoding:
Text File  |  1991-06-28  |  9.5 KB  |  407 lines

  1.     page    132,63,1,1
  2.     opt    rc
  3.     title    'PAM Modulation/Demodulation'
  4.  
  5. ;***************************************************************
  6. ;* PAM.ASM -- PAM Modulation/Demodulation and               *
  7. ;*          SSI drivers                       *
  8. ;*                                   *
  9. ;* Provides interrupt based SSI handling, sample buffering     *
  10. ;* and PAM modulation and demodulation.                *
  11. ;*                                   *
  12. ;* PAM modulation and demodulation is based on some ideas      *
  13. ;* presented in book                           *
  14. ;*    Lee, E., A., Messerschmitt, D., G.:               *
  15. ;*    "Digital Communication",                   *
  16. ;*    Kluwer, 1988                           *
  17. ;*                                   *
  18. ;* Symbol syncronization is based on article               *
  19. ;*    Glisic, S., G.:                        *
  20. ;*    "Symbol Syncronization in Digital Communications       *
  21. ;*     Using Partial Response CPM signaling",                *
  22. ;*    IEEE Trans. on Comm., Vol. 37, No. 3, March 1989       *
  23. ;*                                   *
  24. ;* This module uses registers as follows:               *
  25. ;*  r0 - general purpose (temporary use)               *
  26. ;*  r2 - input sample write pointer                   *
  27. ;*  r3 - output sample read pointer                   *
  28. ;*  r4 - general purpose (temporary use)               *
  29. ;*  r6 - input sample read pointer                   *
  30. ;*  r7 - output sample write pointer                   *
  31. ;*                                   *
  32. ;* Copyright (C) 1991 by Alef Null. All rights reserved.       *
  33. ;* Author(s): Jarkko Vuori, OH2LNS                   *
  34. ;* Modification(s):                           *
  35. ;***************************************************************
  36.  
  37.     nolist
  38.     include 'ioequlc'
  39.     list
  40.  
  41.     section PAM
  42.     xdef    ssi_ini,modem_i
  43.     xdef    m_loop
  44.     xref    putbit,getbit
  45.     xref    m_cra,m_crb,m_tde,m_sr,m_tx
  46.  
  47.     org    p:
  48.  
  49.     nolist
  50.     include 'macros'
  51.     list
  52.  
  53. ; PAM sync parameters
  54. poly    equ    $10010                    ; G3RUH,K9NG scrambler polynomial (x^17 + x^12 + 1)
  55. cM0    equ     6                    ; random walk filter constant
  56. adjlen    equ    18                    ; ADC timing control lenght (in samples)
  57. analen    equ    32                    ; sample quality analyzing lenght
  58.  
  59. ; module status bits
  60. toggle    equ    6                    ; every other bit counter
  61.  
  62. ; first order high-pass IIR filter
  63. ; input in a, output in a
  64. ; frq is -3dB point (fc/fs)
  65. hpass    macro    frq,store
  66.     move            x:<store,x1
  67.     move            a,x:<store
  68.     sub    x1,a        x:<store+1,x0
  69.     move            #-1.0/@cos(6.28319*frq)*(1.0-@sin(6.28319*frq)),x1
  70.     macr    -x0,x1,a
  71.     move            a,x:<store+1
  72.     endm
  73.  
  74.  
  75. ;****************************
  76. ;*    SSI initialization    *
  77. ;****************************
  78. ssi_ini
  79. ; initialize SSI,
  80.     IF    !DEBUG
  81.     movep            #$4000,x:m_cra        ; 16 bit word
  82.     movep            #$f200,x:m_crb        ; syncronous,word frame,ext clk
  83.     ELSE
  84.     movep            #$4011,x:m_cra        ; 16 bit word, abt. 19.2 ksamples/s
  85.     movep            #$f620,x:m_crb        ; syncronous,word frame,ext clk
  86.     ENDIF
  87.  
  88.     rts
  89.  
  90.  
  91. ;****************************
  92. ;*      ADC & MODEM        *
  93. ;*    initialization        *
  94. ;****************************
  95. modem_i
  96. ; program the A/D & D/A converter chip (TLC32044, master clock is 5.184MHz)
  97. ; disable A/D high-pass filter, disable loopback, disable AUX IN,
  98. ; select syncronous mode, select input span 3Vpp, insert sinx/x correction
  99. ; lowpass cutoff frequency at    6840Hz -> TA  =  5 (SCF = 518.4kHz)
  100. ; sampling frequency is        19200Hz -> TB  = 27
  101. ; unit sampling correction time  0.6uS -> TA' =  3
  102.     pgmtlc    %10101000,5,3,27
  103.  
  104. ; sample handling,
  105.     move            #<insamp,r6
  106.     move            #<inN-N,n6
  107.     move            #<inN-1,m6
  108.     move            r6,r2
  109.     move            m6,m2
  110.     movi    0,x:<samque
  111.  
  112.     move            #<outsamp,r3
  113.     move            #<outN-1,m3
  114.     move            #<outsamp+1,r7
  115.     move            m3,m7
  116.     movi    <outfilt,x:<outfptr
  117.  
  118. ; symbol syncro logic,
  119.     movi    cM0,x:<rwalk
  120.     movi    analen,x:<anacnt
  121.     movi    0,x:<goodcnt
  122.  
  123. ; ADC command word
  124.     movi    0,x:<adcnt
  125.  
  126.     rts
  127.  
  128.  
  129. ;****************************
  130. ;*      Main Loop        *
  131. ;****************************
  132. loop    wait
  133. m_loop
  134.  
  135.  
  136. ;****************************
  137. ;*      Receiver        *
  138. ;****************************
  139. ; check if there are new samples received
  140. rec    move            r2,a
  141.     move            r6,x0
  142.     cmp    x0,a        #<rcos,r0
  143.     jeq    <xmit
  144.  
  145. ; yes, there indeed are new samples, check if receiver enabled
  146.     jset    #ptt,x:<status,xmit
  147.  
  148. ; receive squared raised cosine filter
  149.     conv    1,N,r6,r0,a
  150.  
  151. ; remove any DC-level (with high-pass filter)
  152.     hpass    40.0/19200.0,rdc
  153.  
  154. ; limit to binary and queue received bits
  155.     andi    #$fe,ccr
  156.     jmi    <ssi_r00
  157.     ori    #$01,ccr
  158. ssi_r00 move            x:<samque,b1
  159.     rol    b        (r6)+n6
  160.     move            b1,x:<samque
  161.  
  162. ; queue received samples
  163.     move            y:<zsample,b1
  164.     move            b1,y:<dsample
  165.     move            a,y:<zsample
  166.  
  167. ; check if one symbol (2 samples) received
  168.     bchg    #toggle,x:<status
  169.     jcc    <ssi_end
  170.  
  171. ; check if carrier present
  172. ; by checking when eye is open
  173. ; (enought difference between zero crossing sample and decision sample)
  174.     move            y:<dsample,a        ; filter decision samples
  175.     abs    a        #0.005,x1
  176.     move    a,x0
  177.     mpy    x0,x1,a     x:<davg,x0
  178.     move            #0.995,x1
  179.     macr    x0,x1,a
  180.     move            a,x:<davg
  181.  
  182.     move            y:<zsample,b        ; filter zero crossing samples
  183.     abs    b        #0.005,x1
  184.     move    b,x0
  185.     mpy    x0,x1,b     x:<zavg,x0
  186.     move            #0.995,x1
  187.     macr    x0,x1,b     #>2500,x0
  188.     move            b,x:<zavg
  189.  
  190.     sub    b,a                    ; check their difference
  191.     cmpm    x0,a
  192.     jhs    <ssi_r0a
  193.  
  194.     bclr    #dcd,x:<status                ; dcd off
  195.     jcc    <ssi_r0b
  196.     dcdoff
  197.     jsset    #req,x:<status,kickx            ; call persistence routine if needed
  198.     jmp    <ssi_r0b
  199. ssi_r0a bset    #dcd,x:<status                ; dcd on
  200.     jcs    <ssi_r0b
  201.     dcdon
  202.  
  203. ; check if there are transitions (by analyzing the sample between decision samples)
  204. ; (...X|*X|*X|*...)
  205. ;   where X decision sample
  206. ;      * analyzed sample
  207. ;      | symbol boundaries
  208. ssi_r0b move            #>-1,x1
  209.     clr    a        #>1,x0
  210.     jclr    #1,x:<samque,ssi_l1
  211.     jset    #3,x:<samque,ssi_le
  212. ; 0x1
  213.     move            x0,a
  214.     btst    #2,x:<samque
  215.     tcc    x1,a
  216.     jmp    <ssi_le
  217. ssi_l1    jclr    #3,x:<samque,ssi_le
  218. ; 1x0
  219.     move            x1,a
  220.     btst    #2,x:<samque
  221.     tcc    x0,a
  222.  
  223. ; symbol syncro
  224. ssi_le    tst    a        x:<rwalk,x0
  225.     jeq    <ssi_r4
  226.     add    x0,a        #>2*cM0,x0
  227.     jpl    <ssi_r2
  228. ; retard sampling
  229.     move            #cM0,a1
  230.     movib            retard,x:<adccmd
  231.     movib            adjlen,x:<adcnt
  232.     jmp    <ssi_r3
  233. ;
  234. ssi_r2    cmp    x0,a
  235.     jle    <ssi_r3
  236. ; advance sampling
  237.     move            #cM0,a1
  238.     movib            advance,x:<adccmd
  239.     movib            adjlen,x:<adcnt
  240. ;
  241. ssi_r3    move            a1,x:<rwalk
  242.  
  243. ; unscramble symbol, result in C
  244. ssi_r4    btst    #1,x:<samque
  245.     move            x:<usrem,b1
  246.     jcc    <unscram
  247.     move            #>(poly<<1)|1,x0
  248.     eor    x0,b
  249. unscram lsr    b
  250.     move            b1,x:<usrem
  251.  
  252. ; make symbol decision (with NRZ-S decoding), databit in C, result in C
  253.     rol    b        x:<prvrsym,x0
  254.     eor    x0,b        b1,x:<prvrsym
  255.     not    b
  256.     lsr    b
  257.  
  258. ; forward to the HDLC handler
  259.     jsr    <putbit
  260.  
  261. ssi_end jmp    <rec
  262.  
  263.  
  264. ;****************************
  265. ;*     Transmitter        *
  266. ;****************************
  267. ; check if we can calculate new samples
  268. xmit    move            r3,a
  269.     move            r7,x0
  270.     cmp    x0,a
  271.     jeq    <loop
  272.  
  273. ; yes, check if the transmitter is on at all
  274.     clr    a
  275.     jclr    #ptt,x:<status,xmt2
  276.  
  277. ; xmit mode, check if we need a new symbol
  278.     bchg    #toggle,x:<status
  279.     jcc    <xmt1
  280.  
  281. ; yes, check on what phase we are
  282.     jset    #xmt,x:<status,xmt0
  283.  
  284. ; training phase, send continuous preamble (zero pattern)
  285.     andi    #$fe,ccr
  286.     jmp    <morebit
  287.  
  288. ; data phase, get a new bit to xmit
  289. xmt0    jsr    <getbit
  290.     jne    <morebit
  291. ; there is no more bits to send, switch to receiving mode
  292.     jsr    <shutx
  293.     jmp    <scx_end
  294.  
  295. ; NRZ-S coding
  296. morebit rol    a        x:<prvxsym,x0
  297.     not    a
  298.     eor    x0,a        #-1.0,x0
  299.     ror    a        a1,x:<prvxsym
  300.  
  301. ; scrambler, databit in C flag, result in a (-1 or 1)
  302.     clr    a
  303.     rol    a        x:<srem,x0
  304.     eor    x0,a        #-0.9,x1
  305.     lsr    a        #>poly,x0
  306.     jcc    <scram
  307.     eor    x0,a
  308. scram    move            a1,x:<srem
  309.     move            #0.9,a
  310.     tcc    x1,a
  311.  
  312. ; squared raised cosine shape output filter
  313. xmt1    move            x:<outfptr,r4
  314.     move            #<rcos,r0
  315.     move            #<N-1,m4
  316.     move            a,x:(r4)
  317.     conv    1,N,r4,r0,a
  318.     move            r4,x:<outfptr
  319.     move            #-1,m4
  320.  
  321. ; send a new bit
  322.     move            a,x0            ; adjust output level
  323.     move            x:<gain,x1
  324.     mpyr    x0,x1,a     #>$fffc<<8,x0
  325. xmt2    and    x0,a        x:<adcnt,b
  326.     tst    b        #>1,y0            ; check if we must adjust timing
  327.     jeq    <xmt3
  328.     sub    y0,b        x:<adccmd,x0        ; yes, add converter chip control cmds
  329.     or    x0,a        b1,x:<adcnt
  330. xmt3    move            a,x:(r7)+
  331.  
  332. scx_end jmp    <xmit
  333.  
  334.  
  335. ;****************************
  336. ;*     DATA - AREA        *
  337. ;****************************
  338.  
  339.     org    x:
  340.  
  341. adccmd    ds    1                    ; ADC timing command
  342. adcnt    ds    1                    ; ADC timing command counter
  343. samque    ds    1                    ; received sample queue
  344. prvrsym ds    1                    ; previous received symbol
  345. prvxsym ds    1                    ; previous xmitted symbol
  346. rwalk    ds    1                    ; random walk filter counter
  347. davg    ds    1                    ; runnning average
  348. zavg    ds    1                    ; runnning average
  349. rdc    ds    2                    ; DC-level filter
  350. anacnt    ds    1                    ; signal quality analyzing lenght counter
  351. goodcnt ds    1                    ; good symbol counter
  352. usrem    ds    1
  353. srem    ds    1
  354. outfptr ds    1                    ; output filter pointer
  355.  
  356. tmp    ds    1
  357.  
  358.  
  359.     org    y:
  360.  
  361. zsample ds    1                    ; current zero crossing sample
  362. dsample ds    1                    ; previous decision sample
  363.  
  364.     endsec
  365.  
  366.  
  367. ;****************************
  368. ;*    M*BITS MODULO DATA    *
  369. ;****************************
  370.     section PAMData
  371.     xdef    N,inN,outN,rcos
  372.     xdef    insamp,outsamp,outfilt
  373.  
  374.     org    x:
  375.  
  376. ; PAM demod parameters
  377. N    equ    13                    ; samples per filter
  378. inN    equ    16                    ; input buffer lenght
  379. outN    equ    8                    ; output buffer lenght
  380.  
  381. insamp    ds    inN
  382. outfilt ds    N
  383.     ds    16-N
  384. outsamp ds    8
  385.  
  386.     org    y:
  387.  
  388. ; square root of raised cosine pulse filter constants
  389. ; beta = 6/16 = 0.375
  390. rcos    dc    -0.0082167989
  391.     dc     0.0071048900
  392.     dc     0.0254175599
  393.     dc    -0.0592829199
  394.     dc    -0.0434475299
  395.     dc     0.2999202999
  396.     dc     0.5512325000
  397.     dc     0.2999202999
  398.     dc    -0.0434475299
  399.     dc    -0.0592829199
  400.     dc     0.0254175599
  401.     dc     0.0071048900
  402.     dc    -0.0082167989
  403.  
  404.     endsec
  405.  
  406.     end
  407.